package com.gold.security;

import com.alibaba.fastjson.JSONObject;
import com.gold.common.JsonUtils;
import com.gold.common.TokenUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**登录失败自定义
 * @author XiaoHao
 * @date 2022-05-09 13:51
 */
public class AuthFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        //设置返回请求头
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write(JsonUtils.responseJson(false,JsonUtils.ERROR,e.getMessage(),"").toJSONString());
        out.flush();
        out.close();
    }
}
